package br.com.fogas.utils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;

import org.apache.commons.lang3.StringUtils;

public class DateUtility extends org.apache.commons.lang3.time.DateUtils {
	
	public List<Date> getDaysInPeriod(Date init, Date end) {
		init = getDateWithoutHour(init);
		end = getDateWithoutHour(end);
		
		List<Date> daysOfPeriod = new ArrayList<Date>();
		
		Calendar cal = Calendar.getInstance();
		cal.setTime(init);
		
		while(!cal.getTime().equals(end)) {
			daysOfPeriod.add(cal.getTime());
			cal.add(Calendar.DATE, 1);
		}
		daysOfPeriod.add(end);
		
		Collections.sort(daysOfPeriod, new Comparator<Date>() {
			@Override
			public int compare(Date o1, Date o2) {
				return o1.compareTo(o2);
			}
		});
		
		return daysOfPeriod;
	}
	
	public Date getDateByStringAndFormat(final String data, final String format) {
		DateFormat parser = new SimpleDateFormat(format);
		try {
			return parser.parse(data);
		} catch (ParseException e) {
			e.printStackTrace();
		} 
		return null;
	}
	
	public Date getDateWithoutHour(final Date data) {
		DateFormat format = new SimpleDateFormat("dd/MM/yyyy");
		String dtStr = format.format(data);
		try {
			return format.parse(dtStr);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public Calendar getCalendarBydate(final Date date) {
		DateFormat format = new SimpleDateFormat("dd/MM/yyyy");
		String dtFmt = format.format(date);
		int day = Integer.valueOf(StringUtils.substring(dtFmt, 0, 2));
		int month = Integer.valueOf(StringUtils.substring(dtFmt, 3, 5));
		int year = Integer.valueOf(StringUtils.substring(dtFmt, 6, 10));
		
		return getCalendarByDayMonthYear(day, month, year);
	}
	
	public Calendar getCalendarByDayMonthYear(final int day, final int month, final int year) {
		if (month == 1) {
			return new GregorianCalendar(year, Calendar.JANUARY, day);
		} else if (month == 2) {
			return new GregorianCalendar(year, Calendar.FEBRUARY, day);
		} else if (month == 3) {
			return new GregorianCalendar(year, Calendar.MARCH, day);
		} else if (month == 4) {
			return new GregorianCalendar(year, Calendar.APRIL, day);
		} else if (month == 5) {
			return new GregorianCalendar(year, Calendar.MAY, day);
		} else if (month == 6) {
			return new GregorianCalendar(year, Calendar.JUNE, day);
		} else if (month == 7) {
			return new GregorianCalendar(year, Calendar.JULY, day);
		} else if (month == 8) {
			return new GregorianCalendar(year, Calendar.AUGUST, day);
		} else if (month == 9) {
			return new GregorianCalendar(year, Calendar.SEPTEMBER, day);
		} else if (month == 10) {
			return new GregorianCalendar(year, Calendar.OCTOBER, day);
		} else if (month == 11) {
			return new GregorianCalendar(year, Calendar.NOVEMBER, day);
		} else if (month == 12) {
			return new GregorianCalendar(year, Calendar.DECEMBER, day);
		}
		return null;
	}
	
	public Date getDateByLastDayMonth(final Date data) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(data);
		int lastDay = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
		cal.set(Calendar.DAY_OF_MONTH, lastDay);
		
		return getDateWithoutHour(cal.getTime());
	}
	
	public String getStringByDateAndFormat(final Date data,  final String pattern) {
		DateFormat format = new SimpleDateFormat(pattern);
		return format.format(data);
	}
	
	public boolean compareDatesWithoutTime(Date dt1, Date dt2) throws ParseException {
		try {
			DateFormat dtFormat = new SimpleDateFormat("dd/MM/yyyy");
			
			Date d1 = dtFormat.parse(dtFormat.format(dt1));
			Date d2 = dtFormat.parse(dtFormat.format(dt2));
			
			return d1.equals(d2);
		} catch (ParseException e) {
			throw e;
		}
	}
	
	public static boolean isDateBetweenInPeriod(Date init, Date end, Date date) {
		if(date.after(init) && date.before(end)) {
			return true;
		} else if(date.equals(init)) {
			return true;
		} else if(date.equals(end)) {
			return true;
		} else {
			return false;
		}
	}
}